package com.googlecode.jascii.printing;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;

import com.googlecode.jascii.PageResult;
import com.googlecode.jascii.TableResult;
import com.googlecode.jascii.TdResult;
import com.googlecode.jascii.TextStyle;
import com.googlecode.jascii.TrResult;

public class PageResultPrinter implements Printable{

    private PageResult pageResult;
    private int ipage;
    private int fontSize;
	public PageResultPrinter(PageResult pageResult,int fontSize) {
		super();
		this.pageResult = pageResult;
		this.fontSize = fontSize;
	}
	
	/* 
	* Graphic指明打印的圖形環境；PageFormat指明打印頁格式（頁面大小以點為計量單位 
	* 1點為1英寸的1/72，1英寸為25.4毫米。 A4紙大致為595×842點);page指明頁號 
	*/ 
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
			throws PrinterException {
		Graphics2D g2 = (Graphics2D) graphics; 
		g2.setPaint(Color.black); 
		if(pageIndex >= pageResult.getTotalPageNum())
			return Printable.NO_SUCH_PAGE;
		System.out.println("pageIndex="+pageIndex);
		g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
//		Font font = new Font("Serif", Font.PLAIN, fontSize);//設置字型 
		Font fireflyFontDynamic = FontUtils.getFont("fireflysung.ttf");
		Font fireflyFont = fireflyFontDynamic.deriveFont(fontSize  );
		g2.setFont(fireflyFont); 
		PageResult  ptemp = pageResult.getPage(ipage);
		
		int ix=0;
		float y = fontSize;
		for(int iy = 1;iy< ptemp.getDefaultTrNum();iy++)
		{
			TrResult tr = ptemp.getTrResult(iy);
			
			if(tr==null)break;
			String line = tr.toString();
			for(int itemp=0;itemp<line.length();itemp++)
		      {
		    	  char ich = line.charAt(itemp);
		    	  String istr = String.valueOf(ich);
		    	  
		    	  if(istr.getBytes().length==1)
		    	  {
		    		  g2.drawString(String.valueOf(line.charAt(itemp)), (int)ix, (int)y);
		    	      ix+=fontSize/2;
		    	  }
		    	  else
		    	  {
		    		  g2.drawString(String.valueOf(line.charAt(itemp)), (int)ix, (int)y);
		    		  ix+=fontSize;
		    	  }
		    		  
		      }
				
			  y += fontSize;
		      ix=0;
			
		}
		return Printable.PAGE_EXISTS; 
	}
	public void printPage() throws PrinterNotFoundException
	{
		printPage(null,0);
	}
	public void printPage(String prnName ,int page) throws PrinterNotFoundException
	{
		DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; 
		PrintService printService  =  FoundPrinter.getNamedPrintService(prnName) ; 
		DocPrintJob job = printService.createPrintJob(); 
		
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
		//pras.add(MediaSizeName.ISO_A4); 
		pras.add(new Copies(1));
		DocAttributeSet das = new HashDocAttributeSet(); 
		Doc doc = new SimpleDoc(this, flavor, das); 
		if(page!=0)
		{
			try { 
				for(int i=1;i<=pageResult.getTotalPageNum();i++)
				{
					ipage = i;
					System.out.println("ipage="+ipage);
					job.print(doc, pras);
				}
			} catch (PrintException pe) { 
			pe.printStackTrace(); 
			}
		}else
		{
			try { 
				ipage=page;
				job.print(doc, pras);
			} catch (PrintException pe) { 
				pe.printStackTrace(); 
				}
		}
		
	}
	public static void main(String[] args) { 
		TableResult table = new TableResult();
		TextStyle tstyle = table.getTextStyle();
		table.append(new TrResult().append(new TdResult(tstyle.getTopLayout(tstyle.LEFT),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",10,TdResult.TD_ASCII_TYPE,tstyle.getTopLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getTopLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",30,TdResult.TD_ASCII_TYPE,tstyle.getTopLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getTopLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",10,TdResult.TD_ASCII_TYPE,tstyle.getTopLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getTopLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",12,TdResult.TD_ASCII_TYPE,tstyle.getTopLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getTopLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",18,TdResult.TD_ASCII_TYPE,tstyle.getTopLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getTopLayout(tstyle.RIGHT),1,TdResult.TD_ASCII_TYPE ))
				);
		table.append(new TrResult()
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_STYLE_RIGHT))
			.append(new TdResult("財產編號" , 10, TdResult.TD_STYLE_MIDDLE))
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_STYLE_RIGHT))
			.append( new TdResult("號號號號號號號號號號號號", 30 , TdResult.TD_STYLE_LEFT))
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_STYLE_RIGHT))
			.append( new TdResult("明細編號" , 10, TdResult.TD_STYLE_MIDDLE))
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_STYLE_RIGHT))
			.append( new TdResult("0001 - 0001" , 12, TdResult.TD_STYLE_MIDDLE))
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_STYLE_RIGHT))
			.append( new TdResult("2009會計年度" , 18, TdResult.TD_STYLE_MIDDLE))
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_STYLE_RIGHT))
			);
		
		table.append(new TrResult().append(new TdResult(tstyle.getBottomLayout(tstyle.LEFT),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",10,TdResult.TD_ASCII_TYPE,tstyle.getBottomLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getBottomLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",30,TdResult.TD_ASCII_TYPE,tstyle.getBottomLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getBottomLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",10,TdResult.TD_ASCII_TYPE,tstyle.getBottomLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getBottomLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",12,TdResult.TD_ASCII_TYPE,tstyle.getBottomLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getBottomLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",18,TdResult.TD_ASCII_TYPE,tstyle.getBottomLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getBottomLayout(tstyle.RIGHT),1,TdResult.TD_ASCII_TYPE ))
				);
		PageResult page = new PageResult();	
		page.append(table);
//		try {
//			FileUtils.writeStringToFile(new File("page.txt"), page.toString(), "BIG5");
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		PageResultPrinter pl = new PageResultPrinter(page,12);
		try {
			pl.printPage();
		} catch (PrinterNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	} 

}
